package com.hidt.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hidt.shop.domain.Cart;
import com.hidt.shop.domain.Order_item;
import com.hidt.shop.domain.Order_table;
import com.hidt.shop.mapper.Order_tableMapper;
import com.hidt.shop.service.CartService;
import com.hidt.shop.service.Order_itemService;
import com.hidt.shop.service.Order_tableService;
import com.hidt.shop.util.IDUtil;
import com.hidt.shop.util.RestDate;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fhy
 * @Title Order_tableServiceImpl
 * @Package com.hdit.shop.service.impl
 * @Description 订单的业务处理类
 * @date 2021/1/19 14:31
 */
@Service
public class Order_tableServiceImpl implements Order_tableService {
    @Autowired
    private Order_tableMapper order_tableMapper;
    @Autowired
    private Order_itemService orderItemService;
    @Autowired
    private CartService cartService;

    /**
     * 方法描述: 生成完整订单的过程
     * @author Fhy
     * @Date 2021/2/23 9:39
     * @params [order_table]
     * @return com.hidt.shop.util.RestDate
     */
    @Override
    public RestDate addOrderAll(Order_table order_table) {
        System.out.println("开始生成订单");
        int code = 200;
        String mess = "订单生成成功";
//        1、生成订单号
        String order_id = IDUtil.createID();
//        给订单添加订单id
        order_table.setOrder_id(order_id);
//        2、生成订单详情
        for (Order_item order_item:order_table.getOrder_itemList()) {
            /***
             * 商品数量减少
             */

            /***
             * 购物车减少
             */
            Cart cart = new Cart();
            cart.setUser_id(order_table.getOrder_user_id());
            cart.setGoods_id(order_item.getGoods_id());
            int i = cartService.deleteCartByid2(cart);
            if( i != 1) {
                System.out.println("失败");
                code = 101;
                mess = "购物车处理失败";
            }
            /***
             * 生成订单详情
             */
            order_item.setOrder_id(order_id);
            int j = orderItemService.addOrder_item(order_item);
            if( j != 1) {
                System.out.println("添加失败");
                code = 101;
                mess = "生成订单详情失败";
            }
        }
//        3、订单持久化
        String message = addOrder(order_table);

//        4、数据返回
        RestDate restDate = new RestDate();
        restDate.setCode(code);
        restDate.setMess(mess);
        return restDate;
    }

    /**
     * 方法描述: 调用order_table的mapper接口，返回数据插入的状态
     * 1为添加成功，0(或其他)为添加失败
     * @author Fhy
     * @Date 2021/1/19 14:39
     * @params [order_table]
     * @return java.lang.String
     */
    @Override
    public String addOrder(Order_table order_table) {
        System.out.println(order_table);
        int i = order_tableMapper.addOrder(order_table);
        System.out.println(i);
        return judgment(i,"订单生成");
    }
    /**
     * 方法描述:
     * @author Fhy
     * @Date 2021/1/20 14:19
     * @params []
     * @return int
     */
    @Override
    public int findAllOrderPaging() {
        int i = order_tableMapper.findAllOrderPaging();
        return i;
    }

    /**
     * 方法描述: 查询所有订单
     * @author Fhy
     * @Date 2021/1/19 21:08
     * @params []
     * @return java.util.List<com.hidt.shop.domain.Order_table>
     */
    @Override
    public List<Order_table> findAllOrder(Map<String,Integer> map) {
        List<Order_table> order_tableList = order_tableMapper.findAllOrder(map);
        return order_tableList;
    }

    /**
     * 方法描述: 阿里分页练习
     * @author Fhy
     * @Date 2021/2/25 21:15
     * @params [pageNum, pageSize]
     * @return com.github.pagehelper.PageInfo<com.hidt.shop.domain.Order_table>
     */

    @Override
    public PageInfo<Order_table> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Order_table> order_tableList = order_tableMapper.findAll();
        PageInfo<Order_table>   pageInfo=new PageInfo<>(order_tableList,2);

        return pageInfo;
    }

    /**
     * 方法描述:
     * @author Fhy
     * @Date 2021/1/20 14:20
     * @params [userId]
     * @return int
     */
    @Override
    public int findOrderByUserIdPaging(int order_user_id) {
        int i = order_tableMapper.findOrderByUserIdPaging(order_user_id);
        return i;
    }

    /**
     * 方法描述: 查询对应id订单
     * @author Fhy
     * @Date 2021/1/19 21:08
     * @params [userid]
     * @return java.util.List<com.hidt.shop.domain.Order_table>
     */
    @Override
    public List<Order_table> findOrderByUserId(Map<String,Integer> map) {
        List<Order_table> order_tableList = order_tableMapper.findOrderByUserId(map);
        return order_tableList;
    }

    /**
     * 方法描述: 删除对应id订单（逻辑删）
     * @author Fhy
     * @Date 2021/1/19 21:09
     * @params [order_id]
     * @return java.lang.String
     */
    @Override
    public String deleteOrderById(String order_id) {
        int i = order_tableMapper.deleteOrderById(order_id);
        return judgment(i, "订单删除");
    }

    /**
     * 方法描述: 修改对应id订单状态
     * @author Fhy
     * @Date 2021/1/19 21:08
     * @params [order_id, order_status]
     * @return java.lang.String
     */
    @Override
    public String updateOrderStatus(String order_id, int order_status) {
        int i = order_tableMapper.updateOrderStatus(order_id,order_status);
        return judgment(i, "订单状态修改");
    }

    @Override
    public Order_table findOrderDetail(String order_id) {
        Order_table order_table = order_tableMapper.findOrderDetail(order_id);
        return order_table;
    }

    /**
     * 方法描述: 简单的返回字符串判断
     * @author Fhy
     * @Date 2021/1/19 21:08
     * @params [i, str]
     * @return java.lang.String
     */
    private String judgment(int i, String str) {
        if (i == 1) {
            return str + "成功";
        } else {
            return str + "失败";
        }
    }
}
